package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanProjectDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.entity.User;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 护理计划表 服务实现类
 * </p>
 *
 * @author 聂德祥
 * @since 2024-08-15
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Override
    public PageResponse<NursingPlan> NursingPlanAllPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        Page<NursingPlan> nursingPlanPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<NursingPlan> nursingPlanLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nursingPlanLambdaQueryWrapper.like(StrUtil.isNotEmpty(name),NursingPlan::getPlanName,name).eq(status!=null,NursingPlan::getStatus,status);
       nursingPlanPage = nursingPlanMapper.selectPage(nursingPlanPage, nursingPlanLambdaQueryWrapper);
        nursingPlanPage.getRecords().stream().forEach(np->{
            Long createBy = np.getCreateBy();
            if (createBy!=null){
                User user = userMapper.selectById(createBy);
                np.setCreator(user.getRealName());
            }
        });
        return new PageResponse(nursingPlanPage);
    }
@Transactional
    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        // TODO 创建人
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(1);
         nursingPlanMapper.insert(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.stream().forEach(ppp->{
            ppp.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.insert(ppp);
        });
    }

    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.copyProperties(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> projectPlans = getBaseMapper().findNursingProjectPlanVoListById(nursingPlan.getId());
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }
@Transactional
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
    NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
    getBaseMapper().updateById(nursingPlan);
    LambdaQueryWrapper<NursingProjectPlan> nursingProjectPlannLambdaQueryWrapper = new LambdaQueryWrapper<NursingProjectPlan>().eq(nursingPlanDto.getId()!=null,NursingProjectPlan::getPlanId,nursingPlanDto.getId());
    nursingProjectPlanMapper.delete(nursingProjectPlannLambdaQueryWrapper);
    nursingPlanDto.getProjectPlans().stream().forEach(e->{
        e.setPlanId(nursingPlan.getId());
        nursingProjectPlanMapper.insert(e);
    });
    }
@Transactional
    @Override
    public void deleteById(Long id) {
    LambdaQueryWrapper<NursingProjectPlan> eq = new LambdaQueryWrapper<NursingProjectPlan>().eq(id != null, NursingProjectPlan::getPlanId, id);
    nursingProjectPlanMapper.delete(eq);
    getBaseMapper().deleteById(id);
    }
}
